# Maintainer: David Runge <dvzrv@archlinux.org>

pkgname=solr
pkgver=9.4.0
pkgrel=1
pkgdesc="Open source enterprise search platform built on Apache Lucene"
arch=(any)
url="https://lucene.apache.org/solr/"
license=(Apache)
depends=(bash 'java-runtime=11')
makedepends=(gradle git groovy 'java-environment=11')
backup=(
  etc/$pkgname/server/jetty-http.xml
  etc/$pkgname/server/jetty-https.xml
  etc/$pkgname/server/jetty-ssl.xml
  etc/$pkgname/server/jetty.xml
  etc/$pkgname/server/webdefault.xml
  etc/$pkgname/$pkgname.in.sh
  etc/$pkgname/$pkgname.xml
  etc/$pkgname/zoo.cfg
)
source=(
  https://archive.apache.org/dist/$pkgname/$pkgname/$pkgver/$pkgname-$pkgver-src.tgz{,.asc}
  $pkgname.service
  $pkgname.sysusers
  $pkgname.tmpfiles
  support-reproducible-builds.patch
  skip-failing-test.patch
  skip-checks-for-git-repo.patch
  allow-using-system-gradle.patch
)
sha512sums=('0af288f1127a5001034f73b910f06e662de010702710f697c52afda226ced9cf475d9a3fb678953c8ac6e199fc5e0737008466e77ebb52ded2c8813e16975a0c'
            'SKIP'
            'd9a5dcc7a30bf449abf9028bc4aa88e196c953d82b884661ec3191d4a06ec50b106c01ecdcd225e3526ab4f4ce5634d6dcd4a03f1e128fdd4c26febb1b742532'
            '06e5e40b96d2b6668790e4b166fc2867b9e694a2c72fd57eec702526e009b8b0495acbe16a5a27e259827477f4783ce87742f1f806254d8a2baec23b0b317058'
            '97252d1ba1e4e211a6b5a038981cbc9d6663a0d7a980b23484f838eebb2f8194571a3f34ea6b92ed0efd4b7f862e020b5e3e8478ad1de1c6832e232d0b20ff6d'
            'd8c3fcf66b9ad0b101e0bcfb025546f7a708afc53c845a05e2346cb9a3659d3f2f8773a5754fefc6fe98693f6ade5e0c6346b710fe2d0c2c55b12fcaed0e1f18'
            '8daad1f07686984d59c459317936a73dfc35599b4e50f0c2bab769b42950117aae56e775f028c5241c877c771241d9fc5666cc57500a4e94576bfd30be421fe9'
            '1ad7eb67116fc58e7ad38926a696e76f9bc10447101e95ddfb0da914cb7a9916aeab47ad498fec3ff18285827f21d8855c1854778ce09c8e70dc9ce1576b57d1'
            '5e45fcd63be50484dbdd7b909f187d4d90a89639b80571bd93c432599b2064498344d4168dd06dfc45368ce043a873ae6be804d3ff1a3c42746b045c215cab92')
b2sums=('c9babbdade30cf9f99277b0aaa721321189c6c2a4742f2c3096661f631f83bf2562ffcffafc3604bf8b0291ebac6cf2f733ed05c9b1b1c18758c7b8616afe604'
        'SKIP'
        'f970329fad0358cb19af9b9337047612d5949af8aea2dc7acd6c8424ba494644940d22f46b98a9cf1362ea2e5d966b863907de35e7c0edc2e48f7b0ed00473b7'
        '60ff37059a4ab8362551518d56ee105e7d19199727605d5ad6f3236bd31dde1cc5fa37ffa37009820ee3115da36ae64df4754454cef1db51d1c13cac039245cb'
        '37ab5d9af1da1178fcc58ce39654fdecb842b24d4e2264a2eec95c10223e7d003f27a6b2957a267a17ce0fa72258d96642d5b909963576a5a7d13e4e05c47a51'
        'f9a7922b200942d0cf42920ffe9b39180dd7d370074c8d41c89d7096baac3dacd9ce4eb00d36f698ed5d27ef0e75e822c722029c93c22fd8c68a38bfe64ac42a'
        '21c65f14aa76a8110fed7d7b684c22342c72510376f68c2cde5ebf4ea996f38e287a29429ac7ebd93b68dbbffe030149b31d06a1dbc4b80c2182c14c53684c0a'
        '3e7d4e76994752bd62aa6e519bbfecc9db2ad81d3f086ea1920f5bc78bb93c68aab6ca01225860e46e59666af450f3a826fe91edf7a820396be342d431002b94'
        'ef52cfc76c484b78bacfee8a6e6321d80d3a03bdf11f81ab15e8328dd680cc87010e34a3f59423325707f60c87fe03fb2e77a082140e364d61ebaf5582101f59')
# list of trusted signing keys: https://downloads.apache.org/solr/KEYS
validpgpkeys=(
  2085660D9C1FCCACC4A479A3BF160FF14992A24C  # Ishan Chattopadhyaya <ishan@apache.org>
  E58A6F4D5B2B48AC66D5E53BD4F181881A42F9E6  # Ignacio Vera (CODE SIGNING KEY) <ivera@apache.org>
  81D3EB0408B4E1EB10AF443BA4F4C886B29BC2F4  # Alan Woodward (CODE SIGNING KEY) <romseygeek@apache.org>
  86EDB9C33B8517228E88A8F93E48C0C6EF362B9E  # Mike Drob (CODE SIGNING KEY) <mdrob@apache.org>
  38DA0C3CE8181703A08E4D57377C3BA26AD29C0A  # Bruno Roustant <broustant@apache.org>
  50E3EE1C91C7E0CB4DFB007B369424FC98F3F6EC  # Houston Paul Putman IV (CODE SIGNING KEY) <houston@apache.org>
  902CC51935C140BF820230961FD5295281436075  # Jason Gerlowski (CODE SIGNING KEY) <gerlowskija@apache.org>
  7D8D90F8F64F23077AC87CF7CB77CB79928BB0EC  # Atri Sharma <atri@apache.org>
  CFCE5FBB920C3C745CEEE084C38FF5EC3FCFDB3E  # Noble Paul (CODE SIGNING KEY) <noble@apache.org>
  FBC25D7E1712025294FE66590A6AA179B9BBF45E  # Timothy Potter (CODE SIGNING KEY) <thelabdude@apache.org>
  9722F25F650057E26C803B60A6D064D833B3A969  # Mayya Sharipova (CODE SIGNING KEY) <mayya@apache.org>
  E6E21FFCDCEA14C95910EA65051A0FAF76BC6507  # Adrien Grand (CODE SIGNING KEY) <jpountz@apache.org>
  3558857D1F5754B78C7F8B5A71A45A3D0D8D0B93  # Jan Høydahl (CODE SIGNING KEY) <janhoy@apache.org>
  C3E7CBD9B9FE2B419EB14B89612B39A5BC981763  # Michael A. Gibney <magibney@apache.org>
  FDB3D11D716BB32ACF8C93AB919B21537AA80271  # Justin Sweeney (CODE SIGNING KEY) <jsweeney@apache.org>
  2289AC4180E130507D7A82F479C211E0AEFCA72E  # Alex Deparvu <stillalex@apache.org>
)

prepare() {
  cd $pkgname-$pkgver
  patch -Np1 -i "$srcdir"/skip-checks-for-git-repo.patch
  patch -Np1 -i "$srcdir"/support-reproducible-builds.patch
  patch -Np1 -i "$srcdir"/skip-failing-test.patch
  # patch -Np1 -i "$srcdir"/allow-using-system-gradle.patch
}

build() {
  cd $pkgname-$pkgver

  # override adding -SNAPSHOT on version
  # skip signing of jars
  # skip generation of reference guide
  # 
  ./gradlew \
    -Dversion.release=$pkgver \
    assembleRelease \
    -Psign=false -x signJarsPublication \
    -Prefguide.include=false

  install -vdm 755 "$srcdir/$pkgname-$pkgver/dist"
  tar -xf "$pkgname/distribution/build/release/$pkgname-$pkgver.tgz" --strip 1 -C "$srcdir/$pkgname-$pkgver/dist"
}

check() {
  cd $pkgname-$pkgver

  ./gradlew \
    -Dversion.release=$pkgver \
    test \
    -Prefguide.include=false
}

package() {
  local config

  cd $pkgname-$pkgver

  # remove unneeded and OS specific scripts
  rm -rvf dist/bin/{install_solr_service.sh,solr.cmd,solr.in.cmd}

  # make target script executable
  chmod 755 dist/bin/${pkgname}
  # symlink script into PATH
  install -vdm 755 "$pkgdir/usr/bin/"
  ln -sv /usr/share/$pkgname/bin/$pkgname "$pkgdir/usr/bin/$pkgname"

  # configuration
  install -vDm 644 dist/bin/$pkgname.in.sh -t "${pkgdir}/etc/$pkgname"
  install -vDm 644 dist/server/etc/*.xml -t "${pkgdir}/etc/$pkgname/server"
  install -vDm 644 dist/server/$pkgname/$pkgname.xml -t "$pkgdir/etc/$pkgname/"
  install -vDm 644 dist/server/$pkgname/zoo.cfg -t "$pkgdir/etc/$pkgname/"

  # copy application
  install -vdm 755 "$pkgdir/usr/share/$pkgname/"
  cp -dr --no-preserve='ownership' dist/{bin,docs,example,licenses,modules,server} "$pkgdir/usr/share/$pkgname/"

  # symlink configuration into place
  ln -svf /etc/$pkgname/$pkgname.in.sh "$pkgdir/usr/share/$pkgname/"

  install -vdm 750 "$pkgdir/var/lib/$pkgname"
  ln -svf /etc/$pkgname/$pkgname.xml "$pkgdir/var/lib/$pkgname/"
  ln -svf /etc/$pkgname/zoo.cfg "$pkgdir/var/lib/$pkgname/"
  for config in {jetty,jetty-{http,https,ssl},webdefault}.xml; do
    ln -svf /etc/$pkgname/server/$config "$pkgdir/usr/share/$pkgname/server/etc/$config"
  done

  # logs directory
  install -vdm 750 "$pkgdir/var/log/$pkgname/"
  # docs
  install -vDm 644 dist/{CHANGES,NOTICE}.txt -t "$pkgdir/usr/share/doc/$pkgname/"
  install -vDm 644 README.md -t "$pkgdir/usr/share/doc/$pkgname/"
  install -vDm 644 ../$pkgname.service -t "$pkgdir/usr/lib/systemd/system/"
  install -vDm 644 ../$pkgname.sysusers "$pkgdir/usr/lib/sysusers.d/$pkgname.conf"
  install -vDm 644 ../$pkgname.tmpfiles "$pkgdir/usr/lib/tmpfiles.d/$pkgname.conf"
}
